//
//  StudyListTableViewController.m
//  iOSStudy
//
//  Created by Evan Xie on 8/24/15.
//  Copyright (c) 2015 Family Wings. All rights reserved.
//

#import "StudyListTableViewController.h"
#import "FontTableViewController.h"
#import "ColorPickerViewController.h"
#import "Quartz2DViewController.h"


typedef NS_ENUM(NSInteger, StudySection) {
    StudySectionApperance,
    StudySectionFoundation,
    StudySectionImageIO,
    StudySection2DDrawing
};

typedef NS_ENUM(NSInteger, FoundationRow) {
    FoundationRowBackgroundTimer
};

typedef NS_ENUM(NSInteger, ImageIORow) {
    ImageIORowSupportedFormats,
    ImageIORowImageProperties
};

typedef NS_ENUM(NSInteger, ApperanceRow) {
    ApperanceRowColor,
    ApperanceRowFont
};

typedef NS_ENUM(NSInteger, Drawing2DRow) {
    Drawing2DRowRectangleCornerArc,
    Drawing2DRowBezierCurve,
    Drawing2DRowCircleDegree,
    Drawing2DRowBlendMode,
    Drawing2DRowUSFlagByLayer,
    Drawing2DRowPattern,
    Drawing2DRowShadow,
    Drawing2DRowGradient
};


@interface StudyListTableViewController ()
{
    NSArray *_studyList;
}

@end


@implementation StudyListTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _studyList = @[
                   @{ kSectionNameKey: @"Apperance", kItemNamesKey: @[@"Color", @"Font"]},
                   @{ kSectionNameKey: @"Foundation", kItemNamesKey: @[@"Timer Test"]},
                   @{ kSectionNameKey: @"Image IO", kItemNamesKey: @[@"Supported Image Formats", @"Image Properties"]},
                   @{ kSectionNameKey: @"2D Drawing", kItemNamesKey: @[@"Rectangle Corner Arcs", @"Bezier Curve", @"Circle Degree",
                                                                       @"Blend Mode", @"US Flag Using Layer",
                                                                       @"Pattern", @"Shadow", @"Gradient"]},
                  ];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    NSInteger count = _studyList.count;
    NSDictionary *lastInfo = _studyList[count-1];
    NSArray *values = lastInfo[kItemNamesKey];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:values.count-1 inSection:count-1] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _studyList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *studyItems = _studyList[section][kItemNamesKey];
    return studyItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StudyListCell" forIndexPath:indexPath];
    NSArray *studyItems = _studyList[indexPath.section][kItemNamesKey];
    cell.textLabel.text = studyItems[indexPath.row];
    cell.detailTextLabel.text = nil;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *studyContent = _studyList[section];
    return studyContent[kSectionNameKey];
}


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *destViewController = nil;
    switch (indexPath.section) {
        case StudySectionApperance:
            switch (indexPath.row) {
                case ApperanceRowColor:
                    destViewController = [[ColorPickerViewController alloc] init];
                    break;
                case ApperanceRowFont:
                    destViewController = [[FontTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
                default:
                    break;
            }
            break;
            
        case StudySectionFoundation:
            switch (indexPath.row) {
                case FoundationRowBackgroundTimer:
                    destViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TimerTestViewController"];
                    break;
                    
                default:
                    break;
            }
            break;
        case StudySectionImageIO:
            switch (indexPath.row) {
                case ImageIORowSupportedFormats: // Supported Image Formats
                    destViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ImageIOSupportImageFormats"];
                    break;
                    
                case ImageIORowImageProperties:
                    destViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ImagePropertiesController"];
                    break;
                default:
                    break;
            }
            break;
            
        case StudySection2DDrawing:
            
            switch (indexPath.row) {
                case Drawing2DRowRectangleCornerArc:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"RectangleCornerArcView"];
                    break;
                    
                case Drawing2DRowBezierCurve:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"BezierCurveView"];
                    break;
                case Drawing2DRowCircleDegree:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"CircleDegreeView"];
                    break;
                case Drawing2DRowBlendMode:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"BlendModeView"];
                    break;
                case Drawing2DRowUSFlagByLayer:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"USFlagByLayer"];
                    break;
                case Drawing2DRowPattern:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"PatternView"];
                    break;
                case Drawing2DRowShadow:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"ShadowView"];
                    break;
                case Drawing2DRowGradient:
                    destViewController = [[Quartz2DViewController alloc] initWith2DViewClassName:@"GradientView"];
                    break;
                default:
                    break;
            }
            
            break;
            
        default:
            break;
    }
    
    NSDictionary *studyItemInfo = _studyList[indexPath.section];
    destViewController.title = studyItemInfo[kItemNamesKey][indexPath.row];
    [self.navigationController pushViewController:destViewController animated:YES];
}
@end
